<?php
/*
 * EDatePicker class file.
 *
 * @author ironic
 * @version 2.0 beta 5
 * @link http://www.yiiframework.com/
 * @copyright Copyright &copy; 2009 ironic
 * @license dual GPL (3.0 or later) and MIT, at your choice.
 * @license http://www.opensource.org/licenses/mit-license.php
 * @license http://www.opensource.org/licenses/gpl-3.0.php
 *
 * See doc/gpl-3.0.txt and doc/MIT-LICENSE.txt for the full text of the
 * licenses.
 *
 * The MIT license:
 *
 * Copyright (c) 2009 ironic
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * The GPL license:
 *
 * Copyright (C) 2009 ironic
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'EJqueryUiWidget.php');

/**
 *
 * EAccordion: some rails for the jQuery UI widget "Accordion".
 *
 * @see: http://jqueryui.com/demos/accordion/
 * @see: http://docs.jquery.com/UI/Accordion
 *
 * @author: ironic
 * @version: 2.0 beta 4
 * @package: application.extensions.jui
 * @since: 1.0.2
 *
 */
class EAccordion extends EJqueryUiWidget
{
   //***************************************************************************
   // Configuration
   //***************************************************************************

	/**
	 * The panels of the accordion widget.
	 * @var array
	 */
	private $_panels = array();

	/**
	 * The wrapping html tag for the header-part
	 * of a accordion panel.
	 * @var string
	 */
	private $_headerHtml = 'h3';

   //***************************************************************************
   // Internal properties
   //***************************************************************************

	/**
	 * The valid script Options for the accordion widget.
	 * @var array
	 */
	protected $validOptions = array(
		'active'			=> array('type' => 'boolean'), 
		'autoHeight'		=> array('type' => 'boolean'), 
		'clearStyle'		=> array('type' => 'boolean'), 
		'collapsible'		=> array('type' => 'boolean'), 
		'fillSpace'			=> array('type' => 'boolean'), 
		'navigation'		=> array('type' => 'boolean'), 
		'alwaysOpen'		=> array('type' => 'boolean'), 
		'animated'			=> array('type' => 'string'), 
		'event'				=> array('type' => 'string'), 
		'header'			=> array('type' => 'string'), 
		'navigationFilter'	=> array('type' => 'string'),
		'icons'				=> array('type' => 'array'), 
	);

   //***************************************************************************
   // Setters and getters
   //***************************************************************************

   /**
	* Sets the panels property.
	* Format:
	* array(
	*	'Panel 1 Header' => '<p>Panel 1 Content</p>',
	*	'Panel 2 Header' => '<ul><li>Panel 2 Content</li></ul>',
	* )
	* @param array
	*/
	public function setPanels(array $panels)
	{
		$this->_panels = $panels;
	}

   /**
	* Returns the panels property.
	* @return array
	*/
	public function getPanels()
	{
		return $this->_panels;
	}

   /**
	* Sets the header html tag.
	* @param string
	*/
	public function setHeaderHtml($headerHtml)
	{
		if(is_string($headerHtml))
			$this->_headerHtml = $headerHtml;
	}

   /**
	* Returns the header html tag.
	* @return string
	*/
	public function getHeaderHtml()
	{
		return $this->_headerHtml;
	}

   //***************************************************************************
   // Utilities
   //***************************************************************************

   protected function makeOptions()
   {
      $options = array();
		$options['header'] = $this->_headerHtml;
		$options = CJavaScript::encode(array_merge($options, $this->options));     
      return $options;
   }

   /**
	* Generates the javascript code for the widget
	* @return string
	*/
	protected function jsCode($id)
	{
      $options = $this->makeOptions();
		$script = '$("#'.$id.'").accordion('.$options.');';
		return $script;
	}

   /**
	* Generates the html code for the widget
	* @return string
	*/
	public function htmlCode()
	{
		$panelsBuffer = "";
		foreach ($this->_panels as $panelHeader => $panelBody) {
			$header_link = CHtml::link($panelHeader, '#');
			$panelsBuffer .= CHtml::openTag('div')."\n";
			$panelsBuffer .= CHtml::tag($this->_headerHtml, array(), $header_link)."\n";
			$panelsBuffer .= CHtml::tag('div', array(), $panelBody)."\n";
			$panelsBuffer .= CHtml::closeTag('div')."\n";
		}
		return $panelsBuffer;
	}

   //***************************************************************************
   // Run Lola, Run
   //***************************************************************************
	
	/**
	 * Executes the widget.
	 * This method is called by {@link CBaseController::endWidget}.
	 */
	public function run()
	{
		list($name, $id) = $this->resolveNameID();
		
		$this->publishAssets();
		$this->registerClientScripts();

		$this->clientScript->registerScript('Yii.'.get_class($this).'#'.$id, 
											$this->jsCode($name), 
											CClientScript::POS_READY);
				
		echo CHtml::tag('div', array('id'=>$id), $this->htmlCode());
	}
}